package com.sinoglobal.sinostore.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;

import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.util.LogUtils;

/**
 * 
* @title: SignUtil.java 
* @package com.chiliring.sinostore.utils 
* @description: TODO(用一句话描述该文件做什么) 
* @author zrh
* @date 2015年1月16日 下午3:16:57
 */
public class SignUtil {

	public static String generateSign(RequestParams params) {
		String sign="";
		List<NameValuePair>list=params.getQueryStringParams();
		Map<String, String>map=new HashMap<String, String>();
		
		for (NameValuePair nameValuePair:list) {
		    map.put(nameValuePair.getName(), nameValuePair.getValue());
	    }
		Object[] setKey = map.keySet().toArray();
		Arrays.sort(setKey);
		StringBuffer sb=new StringBuffer();
		for(Object result : setKey){
			sb.append(result).append("=")
			.append(map.get(result))
			.append("&");
		}
		sign=sb.substring(0,sb.lastIndexOf("&"));
		LogUtils.d("加密前：sign=="+sign);
		sign=MD5.getMD5Str(sign);
		LogUtils.d("MD5加密后：sign=="+sign);
		sign=RsaUtil.getEncryptText(sign);
		LogUtils.d("RSA加密后：sign=="+sign);
        return sign;
	}
}
